package Graph;



/**
 * @author SASHA
 *
 */



import java.awt.Point;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class Graph {
	public static int maxVertexes = 200;
	public static int graph[][] = new int[maxVertexes][maxVertexes];//matrix of incedent
	public GraphElement mtrx[][] = new GraphElement[maxVertexes][maxVertexes];
	public static int carsOnRoads [][] = new int [maxVertexes][maxVertexes];
	static int no_road = 100000000;
	static int x[] = new int[maxVertexes];
	static int y[] = new int[maxVertexes];
	static float jamsCoefficients [][] = new float [maxVertexes][maxVertexes];
	public static int number;
	static int amount;
	static int svetofor[] = new int[maxVertexes];
	
	public static int Point_Generator(String str)throws FileNotFoundException{
		number = Read_Points(str); 
		Point point[] = new Point[number];
		for(int i = 0; i < number; i++){
			point[i] = new Point(x[i], y[i]);
		}
		amount = number;
		Pointmaker(point);
		return number;
		
    }
	public static Point point[] = new Point[amount];
	
	public static void Pointmaker(Point[] pointx){
		point = pointx; 
	}
	
	public static int  Read_Points(String str) throws FileNotFoundException{
		int i = 0;
		int ix = 0;
		int iy = 0;
		
		FileReader file = new FileReader(str);
		Scanner in = new Scanner(file);
		do{
			
			if((i % 2) == 0){
				x[ix] = in.nextInt();
				ix++;
			}else{
				y[iy] = in.nextInt();
				iy++;
			}
			++i;
		} while (in.hasNextInt());
		in.close();
		return ix;
	}
	
	
	
	public static void Matrix(int number, String matrix) throws FileNotFoundException{
		FileReader file = new FileReader(matrix);
		Scanner in = new Scanner(file);
		do{
			for(int i = 0; i < number; i++){
				for(int j = 0; j < number; j++){
				graph[i][j] = in.nextInt();
				}
			}
		} while(in.hasNextInt());
		in.close();
	}
	
	public static void Make_Matrix(){
		int counter = 0;
		for(int i = 0; i < amount; i++){
			for(int j = 0; j < amount; j++){
				if(graph[i][j] == 0 && i != j){
					   graph[i][j] = no_road;
				}
				if(graph[i][j] == 1){
					graph[i][j] = edge(point[i], point[j]);
				}
				if(graph[i][j] == 5){
					svetofor[counter] = i;
					++counter;
				}
			}
		}
	}
	
	public static int edge(Point p1, Point p2){
		int dx = Math.abs(p1.x - p2.x);
		int dy = Math.abs(p1.y - p2.y);
		double edge = Math.sqrt(dx * dx + dy * dy);
		return (int) edge;
	}
	
	public int[][] getGraph(){
		return graph;
	}
	public Point[] getPoint(){
		return point;
	}
	public int getNumber(){
		return number;
	}
	public int getNoRoad(){
		return no_road;
	}
	public void setGraph(int[][] graph){
		Graph.graph = graph;
	}
	public void setPoint(Point [] point){
		Graph.point = point;
	}
	@SuppressWarnings("static-access")
	public void setNumber(int number){
		this.number = number;
		amount = number;
	}
	
	public GraphElement[][] getMtrx(){
		return mtrx;
	}
	
	public Graph(String Point_Graph, String matrix) throws FileNotFoundException{
		for (int i = 0; i < maxVertexes; i++){
			for (int j = 0; j < maxVertexes; j++){
				mtrx[i][j] = new GraphElement();
			}
		}
		number = Point_Generator(Point_Graph);
		Matrix(number, matrix);
		Graph.Make_Matrix();
	}
	public Graph(){
		for (int i = 0; i < maxVertexes; i++){
			for (int j = 0; j < maxVertexes; j++){
				mtrx[i][j] = new GraphElement();
				jamsCoefficients[i][j] = 1;
			}
		}
	}
	
	
	public void writeFile(int[][] incedent) throws IOException{
		File inc = new File("Incedent");
		File pt = new File("Point_of_Graph");
		
		 FileWriter matr = new FileWriter(inc);
		 FileWriter fpoint = new FileWriter(pt);
		 
		 for(int i = 0; i < number; i++){
			for(int j = 0; j < number; j++){
				matr.write("" + incedent[i][j] + " ");
			}matr.write("\n");
		}
		for(int i = 0; i < number; i++){
			fpoint.write((int) point[i].getX()+" " + (int) point[i].getY() + "\n" );
		}
		matr.flush();
		fpoint.flush();
		matr.close();
		fpoint.close();
	}
	
	public int getmaxVertexes(){
		return maxVertexes;
	}
	public float [][] getJams (){
		return jamsCoefficients;
	}
	public int [][] getCarsOnRoads(){
		return carsOnRoads;
	}
}
